package com.team3.ecommerce.controller;


import com.team3.ecommerce.domain.*;
import com.team3.ecommerce.mapper.OrderMapper;
import com.team3.ecommerce.service.CommodityService;
import com.team3.ecommerce.service.OrderService;
import com.team3.ecommerce.service.UserAuthService;
import com.team3.ecommerce.service.UserInfoService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserAuthService userAuthService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private CommodityService commodityService;

    @GetMapping("/userCenter")
    public String userCenter(Model model, Principal principal) {
        UserAuth userAuth = userAuthService.findUserByPrincipal(principal);//这一行获取了用户id
        UserInfo userInfo = userInfoService.getUserInfo(userAuth.getUserUid());
        model.addAttribute("userInfo", userInfo);
        if(userInfo.getBirthdate()!=null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String birthdate = sdf.format(userInfo.getBirthdate());
            model.addAttribute("birthdate",birthdate);
        }
        else
            model.addAttribute("birthdate","");
        return "userCenter.html";
    }

    @PostMapping("/userCenter")
    public String modifyUserInfo(String birthdate2,UserInfo userInfo, Principal principal) throws ParseException {
        UserAuth userAuth = userAuthService.findUserByPrincipal(principal);
        userInfo.setUserUid(userAuth.getUserUid());
        if(!birthdate2.isEmpty()){
            DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
            Date birthdate = fmt.parse(birthdate2);
            userInfo.setBirthdate(birthdate);
        }
        userInfoService.setUserInfo(userInfo);
        return "redirect:/userCenter";
    }

    @GetMapping("/userCenter/RecAddress")
    public String userCenterRecAdd(Model model, Principal principal) {
        UserAuth userAuth = userAuthService.findUserByPrincipal(principal);
        UserInfo userInfo = userInfoService.getUserInfo(userAuth.getUserUid());
        model.addAttribute("userInfo", userInfo);
        return "userCenter_receiveInfo.html";
    }

    @PostMapping("/userCenter/RecAddress")
    @ResponseBody
    public Map<String, Object> changeRecAdd(@ModelAttribute("UserInfo") UserInfo userInfo, Principal principal) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        UserAuth userAuth = userAuthService.findUserByPrincipal(principal);
        userInfo.setUserUid(userAuth.getUserUid());
        userInfoService.changeRecAddress(userInfo);
        resultMap.put("result", "success");
        return resultMap;
    }

    @RequestMapping("/userCenter/myOrders")
    public String userCenterMyOrders(Model model, Principal principal, @RequestParam("page") int page) {

        UserAuth userAuth = userAuthService.findUserByPrincipal(principal);
        UserInfo userInfo = userInfoService.getUserInfo(userAuth.getUserUid());
        int orderSize = orderService.orderSizeOfUser(userInfo);
        int pageCount = orderSize/5;
        if(orderSize==0){
            pageCount+=1;
        }
        if(orderSize%5!=0)
            pageCount+=1;
        if(page<1)
            return "redirect:/userCenter/myOrders?page=1";
        if(page>pageCount)
            return "redirect:/userCenter/myOrders?page="+pageCount;
        List<Order> orderList = orderService.searchOrdersByUserWithinPage(userInfo,(page-1)*5,5);
        List<TOrder> tOrderList = new ArrayList<TOrder>(); //传输给userCenter_orders.html的临时类
        tOrderList.clear();
        for (Order od : orderList) {
            List<CommInOrder> commInOrderList = orderService.getCommListInOrder(od.getOrderUid());
            List<Commodity> commodityList = new ArrayList<Commodity>();
            commodityList.clear();
            for (CommInOrder cio : commInOrderList) {
                commodityList.add(commodityService.findCommodityById(cio.getCommUid()));
            }
            List<TCommodity> tCommodityList = new ArrayList<TCommodity>();
            tCommodityList.clear();
            for (int i = 0; i < commInOrderList.size(); i++) {
                TCommodity tc = new TCommodity();
                tc.setCommName(commodityList.get(i).getCommName());
                tc.setCommHeadImage(commodityList.get(i).getCommHeadImage());
                tc.setCommVersion(commodityList.get(i).getCommVersion());
                tc.setCommPrice(commodityList.get(i).getCommPrice());
                tc.setCommQuantity(commInOrderList.get(i).getCommQuantity());
                tc.setCommTotalPrice();
                tCommodityList.add(tc);
            }
            TOrder tOrder = new TOrder();
            tOrder.setOrderUid(od.getOrderUid());
            tOrder.setOrderPlaceTime(od.getOrderPlaceTime());
            tOrder.setOrderTotalPrice(od.getOrderTotalPrice());
            tOrder.setOrderStatus(od.getOrderStatus());
            tOrder.settCommodityList(tCommodityList);
            tOrderList.add(tOrder);
        }
        model.addAttribute("nowPage",page);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("tOrderList", tOrderList);
        return "userCenter_orders.html";
    }
}

//传输给userCenter_orders.html的临时类
class TOrder {
    public String orderUid;
    public String orderPlaceTime;
    public Float orderTotalPrice;
    public String orderStatus;
    public List<TCommodity> tCommodityList;

    public void setOrderUid(String orderUid) {
        this.orderUid = orderUid;
    }

    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }

    public void setOrderPlaceTime(Date orderPlaceTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒");
        String placeTime = sdf.format(orderPlaceTime);
        this.orderPlaceTime = placeTime;
    }

    public void setOrderTotalPrice(Float orderTotalPrice) {
        this.orderTotalPrice = orderTotalPrice;
    }

    public void settCommodityList(List<TCommodity> tCommodityList) {
        this.tCommodityList = tCommodityList;
    }
}

//TOrder里的属性 放商品的个别信息
class TCommodity {
    public String commName;
    public Integer commQuantity;
    public Float commPrice;
    public Float commTotalPrice;
    public String commHeadImage;
    public String commVersion;

    public void setCommTotalPrice(Float commTotalPrice) {
        this.commTotalPrice = commTotalPrice;
    }

    public void setCommHeadImage(String commHeadImage) {
        this.commHeadImage = commHeadImage;
    }

    public void setCommVersion(String commVersion) {
        this.commVersion = commVersion;
    }

    public void setCommName(String commName) {
        this.commName = commName;
    }

    public void setCommQuantity(Integer commQuantity) {
        this.commQuantity = commQuantity;
    }

    public void setCommPrice(Float commPrice) {
        this.commPrice = commPrice;
    }

    public void setCommTotalPrice() {
        this.commTotalPrice = this.commQuantity * this.commPrice;
    }
}
